package com.zx.server.util;


import java.util.HashMap;
import java.util.Map;

/**
 * 用于生成Controller返回给客户端的值
 *
 * @author LK
 */
public class ResultHelper {

    /**
     * 用于生成Controller返回给客户端的值
     *
     * @param code
     * @param message
     * @param data
     * @return
     */
    public static ResponseModel createResult(Integer code, String message, Object data) {
        ResponseModel res = new ResponseModel();
        res.setCode(code);
        res.setMessage(message);
        res.setData(data);
        return res;
    }

    public static ResponseModel createResult(Integer code, String message) {
        ResponseModel res = new ResponseModel();
        res.setCode(code);
        res.setMessage(message);
        res.setData(null);
        return res;
    }

    /**
     * 创建只携带成功状态的数据的响应模型
     *
     * @param data 数据
     * @return
     */
    public static ResponseModel createOKResult(Object data) {
        ResponseModel res = new ResponseModel();
        res.setCode(ServiceCode.SUCCESS);
        res.setMessage("");
        res.setData(data);
        return res;
    }

    /**
     * 创建只携带成功状态的分页数据的响应模型
     *
     * @param data 数据
     * @return
     */
    public static ResponseModel createOKPageResult(ResponsePageUtil pageData, Object data) {
        ResponseModel res = new ResponseModel();
        res.setCode(ServiceCode.SUCCESS);
        res.setMessage("");
        Map<Object, Object> map = new HashMap<Object, Object>();
        map = EntityUtils.entityGetMethodToMap(pageData);
        map.put("list", data);
        res.setData(map);
        return res;
    }

    /**
     * 用于生成Controller返回给客户端的值 <strong>只会设置除Data的数据</strong>
     *
     * @param info
     * @return
     */
    public static <T> ResponseModel createResult(ServerResult<T> info) {
        ResponseModel res = new ResponseModel();
        res.setCode(info.getCode());
        res.setMessage(info.getMessage());
        return res;
    }
}
